import React from 'react';

import type { ActionType } from '@ant-design/pro-components';

/**
 * PProTable 通用 locale 配置
 */
export interface TableLocaleOptions {
  /** 空状态描述文字 */
  emptyDescription?: string;
  /** 是否显示刷新按钮 */
  showRefreshButton?: boolean;
  /** 刷新按钮文字 */
  refreshButtonText?: string;
  /** 自定义刷新回调 */
  onRefresh?: () => void;
  /** ActionRef 引用，用于刷新表格 */
  actionRef?: React.RefObject<ActionType | null>;
  /** 自定义空状态内容 */
  customEmptyContent?: React.ReactNode;
}

/**
 * 获取 PProTable 的通用 locale 配置
 * @param options 配置选项
 * @returns locale 配置对象
 */
export const getTableLocale = (options: TableLocaleOptions = {}) => {
  const {
    emptyDescription = '暂无数据',
    showRefreshButton = false,
    refreshButtonText = '刷新数据',
    onRefresh,
    actionRef,
    customEmptyContent,
  } = options;

  // 如果提供了自定义内容，直接使用
  if (customEmptyContent) {
    return {
      emptyText: customEmptyContent,
    };
  }

  // 默认的空状态内容
  const defaultEmptyContent = (
    <AEmpty image={AEmpty.PRESENTED_IMAGE_SIMPLE} description={emptyDescription}>
      {showRefreshButton && (
        <AButton
          type="primary"
          onClick={() => {
            if (onRefresh) {
              onRefresh();
            } else if (actionRef?.current) {
              actionRef.current.reload();
            }
          }}
        >
          {refreshButtonText}
        </AButton>
      )}
    </AEmpty>
  );

  return {
    emptyText: defaultEmptyContent,
  };
};

/**
 * 常用的 locale 配置预设
 */
export const TableLocalePresets = {
  /** 设备列表 */
  device: (actionRef?: React.RefObject<ActionType | null>) =>
    getTableLocale({
      emptyDescription: '暂无设备数据',
      actionRef,
    }),

  /** 产品列表 */
  product: (actionRef?: React.RefObject<ActionType | null>) =>
    getTableLocale({
      emptyDescription: '暂无产品数据',
      actionRef,
    }),

  /** 用户列表 */
  user: (actionRef?: React.RefObject<ActionType | null>) =>
    getTableLocale({
      emptyDescription: '暂无用户数据',
      actionRef,
    }),

  /** 订单列表 */
  order: (actionRef?: React.RefObject<ActionType | null>) =>
    getTableLocale({
      emptyDescription: '暂无订单数据',
      actionRef,
    }),

  /** 日志列表 */
  log: (actionRef?: React.RefObject<ActionType | null>) =>
    getTableLocale({
      emptyDescription: '暂无日志数据',
      actionRef,
    }),

  /** 通用列表（只显示描述，不显示刷新按钮） */
  readonly: (description: string = '暂无数据') =>
    getTableLocale({
      emptyDescription: description,
      showRefreshButton: false,
    }),

  /** 搜索结果为空 */
  searchEmpty: (actionRef?: React.RefObject<ActionType | null>) =>
    getTableLocale({
      emptyDescription: '没有找到匹配的数据',
      refreshButtonText: '重置搜索',
      actionRef,
    }),

  /** 权限不足 */
  noPermission: () =>
    getTableLocale({
      emptyDescription: '暂无权限查看数据',
      showRefreshButton: false,
    }),

  /** 网络错误 */
  networkError: (actionRef?: React.RefObject<ActionType | null>) =>
    getTableLocale({
      emptyDescription: '网络连接失败，请重试',
      refreshButtonText: '重新加载',
      actionRef,
    }),
};

/**
 * 创建带有统一 locale 的 PProTable 配置
 * @param actionRef ActionRef 引用
 * @param localeType 预设类型或自定义配置
 * @returns 包含 locale 的配置对象
 */
export const createTableConfig = (
  actionRef: React.RefObject<ActionType | null>,
  localeType: keyof typeof TableLocalePresets | TableLocaleOptions = 'device'
) => {
  let locale;

  if (typeof localeType === 'string') {
    // 使用预设配置
    locale = TableLocalePresets[localeType](actionRef);
  } else {
    // 使用自定义配置
    locale = getTableLocale({ ...localeType, actionRef });
  }

  return {
    locale,
    // 可以在这里添加其他通用配置
    pagination: {
      defaultPageSize: 10,
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: (total: number, range: [number, number]) =>
        `共 ${total} 项数据，当前显示第 ${range[0]}-${range[1]} 项`,
    },
    scroll: { x: 1000 },
    size: 'small' as const,
  };
};

/**
 * Hook: 使用统一的表格配置
 * @param localeType 预设类型或自定义配置
 * @returns [actionRef, tableConfig]
 */
export const useTableConfig = (
  localeType: keyof typeof TableLocalePresets | TableLocaleOptions = 'device'
) => {
  const actionRef = React.useRef<ActionType>(null);
  const tableConfig = createTableConfig(actionRef, localeType);

  return [actionRef, tableConfig] as const;
};
